﻿using System;
using System.Collections.Generic;
using System.Linq;
using XcoAppSpaces.Contracts.Logging;
using XcoAppSpaces.Contracts.Service;
using XcoAppSpaces.Contracts.Transport;
using XcoAppSpaces.Core.Configuration;
using XcoAppSpaces.Logging.TraceSource;

namespace XcoAppSpaces.Core
{
	/// <summary>
	/// Class for statically initializing platform specific appspace configuration.
	/// This includes options for assembly loading, logging, default services and default config string interpreters.
	/// </summary>
	public static class XcoAppSpacePlatfromSpecificConfig
	{
		private static bool isInitialized = false;

		/// <summary>
		/// Intializes the platform specific appspace configuration. This is also automatically called the first time
		/// the XcoAppSpace class is used, so normally doesn't need to be called explicitly.
		/// </summary>
		public static void Init()
		{
			if (isInitialized)
				return;
			isInitialized = true;

			//do some Platform-specific initializiation
			TypeResolve.Loader_Net45.AssemblyLoader.ConfigurePlatformBasedAssemblyLoading();
			XcoLoggerFactory.CreateInstance = (t => new TraceSourceLogger(t.Namespace, System.Diagnostics.SourceLevels.Error));
			DefaultServices.SetDefault<IXcoTransportService>(() => new XcoAppSpaces.Transport.Sockets.XcoTCPTransportService());
			DefaultInterpreters.GetDefaultConfigStringInterpreters = () => new List<IXcoConfigStringInterpreter>
				{
					new XcoAppSpaces.Transport.Sockets.ConfigStringInterpreter(),
#if !PocketPC && !SILVERLIGHT
					new XcoAppSpaces.Core.Interpreters.SerializerConfigStringInterpreter(),
					new XcoAppSpaces.Transport.WCF.ConfigStringInterpreter(),
					new XcoAppSpaces.Transport.Jabber.ConfigStringInterpreter(),
					new XcoAppSpaces.Transport.MSMQ.ConfigStringInterpreter(),
					new XcoAppSpaces.Security.Basic.ConfigStringInterpreter(),
#endif
				};
		}
	}
}
